package com.barbeque.mall.service;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.param.User;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.bean.user.MarketUserExample;

import com.barbeque.mall.mapper.MarketUserMapper;
import com.barbeque.mall.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    MarketUserMapper marketUserMapper;


    @Override
    public CommonData<User> query(BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        List<User> users = userMapper.select(info.getSort(), info.getOrder());
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return CommonData.data(pageInfo);
    }


    @Override
    public void updateUser(MarketUser marketUser) {
        marketUserMapper.updateByPrimaryKey(marketUser);
    }

    @Override
    public MarketUser selectUserById(int id) {
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);

        return marketUser;
    }

    /**
     * 显示用户信息和查询
     *
     * @param username
     * @param mobile
     * @param info
     * @return
     */
    @Override
    public CommonData<MarketUser> selectUsers(String username, String mobile, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        marketUserExample.setOrderByClause(info.getSort() + " " + info.getOrder());

        if (username != null ){
            criteria.andUsernameLike("%"+username+"%");
        }
        if (mobile != null){
            criteria.andMobileLike("%"+mobile+"%");
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);

        return CommonData.data(marketUserPageInfo);
    }


}
